package jobhunter.controller;

import java.io.UnsupportedEncodingException;

import jobhunter.model.Contact;
import jobhunter.model.User;
import jobhunter.repository.CompanyRepository;
import jobhunter.repository.ContactRepository;
import jobhunter.service.ContactService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class ContactController 
{
	@Autowired
	private ContactRepository contactRepository;
	@Autowired
	private ContactService contactService;
	@Autowired
	private CompanyRepository companyRepository;

	@RequestMapping("/contactsList")
	public ModelAndView listContact()
	{
		ModelAndView mav = new ModelAndView("contact/contactsList");
		mav.addObject("contactsList", contactRepository.findByUser());
		return mav;
	}

	@RequestMapping("/contact")
	public ModelAndView contact(@RequestParam(value = "id") Long id,@RequestParam(value = "e", required = false) Long e)
	{
		Contact contact = contactRepository.findByIdWithActions(id);	
		ModelAndView mav = new ModelAndView("contact/contact");
		mav.addObject("contact",contact);
		mav.addObject("typeError",e);
		return mav;
	}

	@RequestMapping("/delContact")
	public String delContact(@RequestParam(value = "id") Long id,
			@RequestParam(value = "compId", required = false) Long compId)
	{
		try
		{
			contactRepository.remove(id);
			if(compId != null )
				return "redirect:company?id="+compId;
			else
				return "redirect:contactsList";
		}
		catch(Exception e)
		{
			return "redirect:contact?id="+id+"&e=1";
		}
	}
	
	@RequestMapping("/addContact")
	public ModelAndView addContact(@RequestParam(value = "compId", required = false) Long compId)
	{		
		ModelAndView mav = new ModelAndView("contact/addContact");
		if(compId != null)
		{
			mav.addObject("company",companyRepository.findById(compId));
		}
		else
		{
			mav.addObject("companies",companyRepository.findByUser());			
		}
		return mav;
	}

	@RequestMapping("/addContactValidate")
	public String addContactValidate(@RequestParam(value = "firstName") String firstName,
			@RequestParam(value = "lastName") String lastName,
			@RequestParam(value = "phone") String phone,
			@RequestParam(value = "email") String email,
			@RequestParam(value = "function") String function,
			@RequestParam(value = "description") String description,
			@RequestParam(value = "companyId") Long companyId,
			@RequestParam(value = "page", required = false) String page) throws UnsupportedEncodingException
	{
		

		firstName = new String(firstName.getBytes("ISO-8859-1"),"UTF-8");
		lastName = new String(lastName.getBytes("ISO-8859-1"),"UTF-8");
		phone = new String(phone.getBytes("ISO-8859-1"),"UTF-8");
		email = new String(email.getBytes("ISO-8859-1"),"UTF-8");
		function = new String(function.getBytes("ISO-8859-1"),"UTF-8");
		description = new String(description.getBytes("ISO-8859-1"),"UTF-8");
		
		
		contactService.add(firstName,lastName,phone, email,function,description,companyId);
		if(page==null)
			return "redirect:contactsList";
		else
			return "redirect:company?id="+companyId;
	}


	@RequestMapping("/updContact")
	public ModelAndView updContact(@RequestParam(value = "id") Long id,
			@RequestParam(value = "compId", required = false) Long compId)
	{
		ModelAndView mav = new ModelAndView("contact/updContact");
		mav.addObject("contact",contactRepository.findById(id));
		
		if(compId != null)
		{
			mav.addObject("company",companyRepository.findById(compId));
		}
		else
		{
			mav.addObject("companies",companyRepository.findByUser());			
		}
		return mav;
	}

	@RequestMapping("/updContactValidate")
	public String updContactValidate(@RequestParam(value = "id") Long id,
			@RequestParam(value = "firstName") String firstName,
			@RequestParam(value = "lastName") String lastName,
			@RequestParam(value = "phone") String phone,
			@RequestParam(value = "email") String email,
			@RequestParam(value = "function") String function,
			@RequestParam(value = "description") String description,
			@RequestParam(value = "companyId") Long companyId,
			@RequestParam(value = "page", required = false) String page) throws UnsupportedEncodingException
	{
		firstName = new String(firstName.getBytes("ISO-8859-1"),"UTF-8");
		lastName = new String(lastName.getBytes("ISO-8859-1"),"UTF-8");
		phone = new String(phone.getBytes("ISO-8859-1"),"UTF-8");
		email = new String(email.getBytes("ISO-8859-1"),"UTF-8");
		function = new String(function.getBytes("ISO-8859-1"),"UTF-8");
		description = new String(description.getBytes("ISO-8859-1"),"UTF-8");
		
		
		
		contactService.update(id,firstName,lastName,phone, email,function,description,companyId);
		if(page==null)
			return "redirect:contactsList";
		else
			return "redirect:company?id="+companyId;
	}
		
}